<?php
namespace api\modules\v1\controllers;

use api\modules\v1\models\shop\GoodsCategory;
use api\modules\v1\models\shop\GoodsComment;
use api\modules\v1\models\shop\GoodsCommentRelation;
use api\modules\v1\models\shop\GoodsInfo;
use yii\helpers\ArrayHelper;
use yii\filters\auth\HttpHeaderAuth;
use Yii;
/**
 * Default controller for the `v1` module
 */
class GoodsController extends DefaultController
{

    public $modelClass = 'api\modules\v1\models\GoodsInfo';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法
                    'category-list','goods-list','info','comment-list'
                ]
            ]
        ]);
    }

    //========================== 商品分类 ==============================

    //获取层级分类
    public function actionCategoryList() {
        if (Yii::$app->request->isGet) {
            return (new GoodsCategory())->getList();
        }
    }

    //========================== 商品管理 ==============================
    //商品列表
    public function actionGoodsList() {
        if (Yii::$app->request->isGet) {
            return (new GoodsInfo())->getGoodsList();
        }
    }

    //商品详情
    public function actionInfo($id = 0) {
        if (Yii::$app->request->isGet) {
            return (new GoodsInfo())->getInfo($id);
        }
    }

    //收藏操作
    public function actionGoodsCollection($id = 0) {
        if (Yii::$app->request->isGet) {
            return (new GoodsInfo())->collect($id);
        }
    }


    //我想要
    public function actionGoodsLike($id = 0) {
        if (Yii::$app->request->isGet) {
            return (new GoodsInfo())->like($id);
        }
    }


    //商品评论列表
    public function actionCommentList() {
        if (Yii::$app->request->isGet) {
            return (new GoodsComment())->getCommentList();
        }
    }

    //评论商品
    public function actionComment() {
        if (Yii::$app->request->isPost) {
            return (new GoodsComment())->saveComment();
        }
    }

    //删除评论
    public function actionDeleteComment($id = 0) {
        if (Yii::$app->request->isDelete) {
            return (new GoodsComment())->DeleteComment($id);
        }
    }

   //评论点赞
    public function actionCommentLike()
    {
        if (Yii::$app->request->isPost) {
            return (new GoodsCommentRelation())->like();
        }
    }

    //商户修改商品
    public function actionAddEdit($id = 0) {
        $model = new GoodsInfo();
        if (\Yii::$app->request->isGet) {
            return $model->getGoodsInfo($id);
        }

        if (\Yii::$app->request->isPost) {
            $model->setScenario('addEdit');
            if ($model->load(\Yii::$app->request->post(), '')) {
                return $model->addEditGoods();
            }
        }
    }

    //下架
    public function actionGoodsStatus() {
        if (Yii::$app->request->isGet) {
            return (new GoodsInfo())->getGoodsStatus();
        }
    }

}
